package com.myres.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myres.admin.db.AdminService;
import com.myres.db.ModelService;
import com.myres.model.Order;
import com.myres.model.OrderItem;
import com.myres.model.User;
import com.sun.xml.internal.txw2.output.XMLWriter;

public class CheckNewOrder extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public CheckNewOrder() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
	//	String s=(String)request.getSession().getServletContext().getAttribute("max_order_id");
	//    if(s==null) s="1";
	//	int maxId=Integer.parseInt(s);
	//	int newMaxId=new MessageService().getNewOrderMaxId(maxId);
	//	
	//	if(newMaxId>maxId){
	//		request.getSession().getServletContext().setAttribute("max_order_id", String.valueOf(newMaxId));
	//		response.getWriter().write("yes");
	//	}
	//	else response.getWriter().write("no");
		process(request,  response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//    //    response.setContentType("text/html");
//	//	response.setCharacterEncoding("gbk");
//	//	String s=(String)request.getSession().getServletContext().getAttribute("max_order_id");
//	////    if(s==null) s="1";
//	//	int maxId=Integer.parseInt(s);
//	/	int newMaxId=new MessageService().getNewOrderMaxId(maxId);
//		
//		if(newMaxId>maxId){
//			request.getSession().getServletContext().setAttribute("max_order_id", String.valueOf(newMaxId));
//			response.getWriter().write("yes");
//		}
//		else response.getWriter().write("no");
		process(request,  response);
	}

	public void process(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		
		//String uid=request.getParameter("uid");
		//System.out.print("maxuid="+uid);
		List<Order> newOrders=new AdminService().checkNewOrder();
		ModelService service=new ModelService();
		//List<User> users=service.checkUser(Integer.parseInt(uid));
		if(newOrders.size()==0){
			response.sendError(505);
			return;
		}
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		StringBuilder sb=new StringBuilder();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>");
		sb.append("<root>");
		for(Order order:newOrders){
			sb.append("<order>");
			sb.append("<id>");
			sb.append(order.getId());
			sb.append("</id>");
			sb.append("<no>");
			sb.append(order.getNO());
			sb.append("</no>");
			sb.append("<who>");
			sb.append(order.getSendToWho());
			sb.append("</who>");
			sb.append("<phone>");
			sb.append(order.getReceiverPhone());
			sb.append("</phone>");
			sb.append("<money>");
			sb.append(order.getPayment());
			sb.append("</money>");
			sb.append("<order_time>");
			sb.append(order.getOrderTime());
			sb.append("</order_time>");
			sb.append("<send_time>");
			sb.append(order.getDeadline());
			sb.append("</send_time>");
			sb.append("<word>");
			sb.append(order.getSpecial_want());
			sb.append("</word>");
			sb.append("<state>");
			sb.append(order.getState());
			sb.append("</state>");
			sb.append("<address>");
			sb.append(order.getSendToPlace());
			sb.append("</address>");
			for(OrderItem item:order.getItems()){
				sb.append("<item>");
				sb.append("<name>");
				sb.append(item.getName());
				sb.append("</name>");
				sb.append("<price>");
				sb.append(item.getPrice());
				sb.append("</price>");
				sb.append("<amount>");
				sb.append(item.getAmount());
				sb.append("</amount>");
				sb.append("</item>");
			}
			sb.append("</order>");
			//int count=new AdminService().OrderSeen(order.getId());
			//System.out.print(count);
		}
		
//		for(User user:users){
//	    	  sb.append("<user>");
//	    	  sb.append("<user_name>").append(user.getName()).append("</user_name>");
//	    	  sb.append("<mobile>").append(user.getMobile()).append("</mobile>");
//	    	  sb.append("<address>").append(user.getAddress()).append("</address>");
//	    	  sb.append("<sid>").append(user.getId()).append("</sid>");
//	    	  sb.append("<r>").append(user.getSmsToken()).append("</r>");
//	    	  sb.append("<email>").append(user.getEmail()).append("</email>");
//	    	  sb.append("</user>");
//	      }
		sb.append("</root>");
		/*
		if(newOrders.size()>0){
		  int[] count=new AdminService().OrdersSeen(newOrders);
		  if(count!=null)System.out.print(count.length);
		}
		*/
		Writer writer=response.getWriter();
		writer.write(sb.toString());
		writer.flush();
		writer.close();
	}
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
